package org.example.heterogeneous.readData;

import org.example.basic.AdjacencyEdge;
import org.example.basic.Edge;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * DBLP数据
 */
public class DBLPData {

    private int start = 0x3f3f3f3f;

    private int end = 0;

    // 输入的边
    private List<Edge> edgeList;

    // 每个顶点的core
    private int[] core;

    //邻接表
    private List<AdjacencyEdge> adjacencyEdgeList;

    public void read(String inputFileName, String regex){
        try {
            BufferedReader reader = new BufferedReader(new FileReader(inputFileName));
            String line;

            // 标识边(x,y)是否已经存储
            Map<String, Boolean> edgeExist = new HashMap<>();
            edgeList = new ArrayList<>();
            int u, v, x, y;
            String str;
            while ((line = reader.readLine()) != null) {
                // 不是数字则数据不对
                if (line.charAt(0) > '9' || line.charAt(0) < '0') {
                    continue;
                }
                String[] words = line.split(regex);
                if (words.length != 2) {
                    continue;
                }
                u = Integer.parseInt(words[0].trim());
                v = Integer.parseInt(words[1].trim());
                // 去除自环
                if (u == v) {
                    continue;
                }
                start = Math.min(start, Math.min(u, v));
                end = Math.max(Math.max(u, v), end);

                x = Math.min(u, v);
                y = u ^ v ^ x;
                str = x + "-" + y;
                // 去除重边
                if (edgeExist.containsKey(str)) {
                    continue;
                }
                edgeExist.put(str, true);
                edgeList.add(new Edge(x, y));
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
